<template>
	<view class="wrap bgw">
		<view class="main bg1">
			<view class="user p30">
				<image class="userimg" :src="user.avatarImg"></image>
				<view class="txw">
					<p class="p1 one text-xl">{{ user.username }}</p>
					<p class="p2 one c-grey text-df">
						{{ user.mobile }}
						<span>(  {{ endTime }} 服务到期 )</span>
					</p>
				</view>
			</view>
		</view>
		<view class="list p30">
			<view class="title text-lg">选择服务</view>
			<ul>
				<li @tap="select(v, i)" v-for="(v, i) in tabList" :class="{ act: tabState == i }" class="text-lg ">
					<p class="p1">{{ v.name }}</p>
					<p class="p2 text-xxl">
						<span class="text-sm"></span>
						{{ v.amounts }}
					</p>
					<p class="p3 text-sm c-grey">折合￥{{v.super}}/月</p>
					<!-- <p class="p4 text-sm ">立省￥60</p> -->
					<p v-if=" minNum == i" class="label text-sm">超值</p>
				</li>
			</ul>
		</view>
		<view class="btn p30"><button @tap="send()" class="btnbg">支付宝支付</button></view>
	</view>
</template>

<script>
	import {
		getProduct,
		getEndDate,
		myInfo,
		getOrderStr
	} from '@/api/api.js';
	export default {
		data() {
			return {
				shakeState: 0,
				tabList: [],
				super: 0,
				endTime: '',
				user: [],
				tabState: 0,
				minNum:0,
				id: 0
			};
		},
		created() {
			this.init();
		},
		methods: {
			select(v, i) {
				this.tabState = i;
				this.id = v.productId;
			},
			async send() {
				let ids = {
					paymentBy: 0,
					productId: this.id
				};
				if (this.shakeState == 0) {
					this.shakeState = 1
					let order = await getOrderStr(ids);
					if (order.code == 0) {
						let orderID = order.orderStr;
						uni.requestPayment({
							provider: 'alipay',
							orderInfo: orderID, //微信、支付宝订单数据
							complete: () => {
								this.init();
								this.shakeState = 0
							}
						});
					} else {
						this.shakeState = 0
					}
				}
			},
			async init() {
				let data = await myInfo();
				this.user = data.user;
				let endTime = await getEndDate();
				endTime = endTime.endDate.substring(0, 10);
				this.endTime = endTime;
				let list = await getProduct();
				list = list.productList;
				let maxNum = 1000000000;
				let minNum = 0;
				for (let i = 0; i < list.length; i++) {

					list[i].super = list[i].amounts / list[i].months;


					var num = list[i].super;
					num = Math.round(num * 100) / 100 
					list[i].super = num

					if (maxNum > list[i].super) {
						maxNum = list[i].super;
						minNum = i;
					}
				}
				this.id = list[minNum].productId;
				this.tabState = minNum;
				this.minNum = minNum
				this.tabList = list;
			}
		}
	};
</script>

<style lang="less" scoped>
	@import './index.less';
</style>
